#include "chooselevelscene.h"
#include<QMenuBar>
#include<QPainter>
#include<QTimer>
#include<QLabel>
#include<QDebug>
#include"mypushbutton.h"
#include"playscene.h"
#include<QSoundEffect>
chooselevelscene::chooselevelscene(QWidget *parent)
    : QMainWindow{parent}
{
    //配置选择关卡场景
    this->setFixedSize(320,588);
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));
    this->setWindowTitle("选择关卡");
    //创建菜单栏
    QMenuBar *bar=new QMenuBar();
    setMenuBar(bar);
    QMenu *startMenu=bar->addMenu("开始");
    QAction *quitAction=startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });
    //创建音效
    QSoundEffect *closeSound=new QSoundEffect();
    //创建关闭按钮
    MyPushButton *closeBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    closeBtn->setParent(this);
    closeBtn->move(this->width()-closeBtn->width(),this->height()-closeBtn->height());
    //创建按钮监听
    connect(closeBtn,&MyPushButton::clicked,[=](){
        closeSound->setSource(QUrl::fromLocalFile(":/res/BackButtonSound.wav"));
        closeSound->play();
        QTimer::singleShot(500,this,[=](){
            emit this->chooseCloseScene();
        });
    });
    QSoundEffect *chooseSound=new QSoundEffect();
    //创建选择关卡按钮
    for(int i=0;i<20;i++)
    {
        MyPushButton *menuBtn=new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25+i%4*70,130+i/4*70);
        //监听每个按钮得事件

        connect(menuBtn,&MyPushButton::clicked,[=](){
            // QString str=QString("你选择的是第%1关").arg(i+1);
            // qDebug()<<str;
            chooseSound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));
            chooseSound->play();
            if(pScene==NULL)
                {
                this->hide();
                pScene=new PlayScene(i+1);
                pScene->show();
            }
            connect(pScene,&PlayScene::playCloseScene,[=](){
                this->show();
                delete pScene;
                pScene=NULL;
            });
        });

        QLabel *label=new QLabel();
        label->setParent(this);
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i+1));
        label->move(25+i%4*70,130+i/4*70);
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        //设置鼠标穿透
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }
}

void chooselevelscene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/background2.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //画图标
    pix.load(":/res/Title.png");
    //pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix);

}
